package mycoffee.order.task;

import java.util.ArrayList;

import mycoffee.order.activity.MyCoffeeApplication;
import mycoffee.order.database.DatabaseConnection;
import mycoffee.order.model.Category;
import mycoffee.order.model.Food;
import mycoffee.order.model.SubCategory;
import android.content.Context;

/**
 * 
 * @author Tuong
 * 
 */
public class FoodsTask extends AbstractTask<Void, Void, ArrayList<Category>> {

	private static final String TAG = "FoodsTask";
	DatabaseConnection dbCon = MyCoffeeApplication.mDatabaseConnection;
	public FoodsTask(Context context,
			IDataEventHandler<ArrayList<Category>> dataEventHandler) {
		super(context, dataEventHandler);
	}

	@Override
	protected ArrayList<Category> doExecute() {

		Category mCurrentCategory = null;
		SubCategory mCurrentSubCategory = null;
		ArrayList<Category> categories = dbCon.getListCategory();
		ArrayList<SubCategory> subcategories = null;
		ArrayList<Food> foods = null;
		Food curFood = null;
		try {
			for ( int i = 0; i < categories.size(); i++ ) {
				mCurrentCategory = categories.get(i);
				subcategories = dbCon.getListSubCategory(mCurrentCategory.getId());
				mCurrentCategory.setSubCategories(subcategories);
				for ( int j = 0; j < subcategories.size(); j++ ) {
					mCurrentSubCategory = subcategories.get(j);
					foods =  dbCon.getListFood( mCurrentSubCategory.getId() );
					for ( int k = 0; k < foods.size(); k++ ) {
						curFood = foods.get(k);
						curFood.setDepartment(mCurrentCategory.getDepartment());
					}
					mCurrentSubCategory.setFoods(foods);
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return categories;
	}

}
